import html2canvas from 'html2canvas'


export function GetParams(params) {
  let urlP = "?";
  for (const key in params) {
    if (Object.hasOwnProperty.call(params, key)) {
      const element = params[key];
      urlP += `&${key}=${element}`;
    }
  }
  return urlP
}
// url直接导出处理
export function exportFileByUrl(url, filename) {
  var tempLink = document.createElement('a') // 创建一个a指标
  tempLink.style.display = 'none'
  tempLink.href = url
  tempLink.setAttribute('download', filename) // 给a指标添加下载属性
  if (typeof tempLink.download === 'undefined') {
    tempLink.setAttribute('target', '_blank')
  }
  document.body.appendChild(tempLink) // 将a指标添加到body当中
  tempLink.click() // 启动下载
  document.body.removeChild(tempLink) // 下载完毕删除a指标
  window.URL.revokeObjectURL(url)
}

// 二进制流导出处理
export function exportFile(response, filename, excelType) {
  console.log(response, 'response')
  console.log(filename, 'filename')
  console.log(excelType, 'excelType')
  const blob = new Blob([response], {
    type: excelType
  }) // 转化为blob对象
  // const blob = response
  if (typeof window.navigator.msSaveBlob !== 'undefined') {
    window.navigator.msSaveBlob(blob, filename)
  } else {
    var blobURL = window.URL.createObjectURL(blob) // 将blob对象转为一个URL
    var tempLink = document.createElement('a') // 创建一个a指标
    tempLink.style.display = 'none'
    tempLink.href = blobURL
    tempLink.setAttribute('download', filename) // 给a指标添加下载属性
    if (typeof tempLink.download === 'undefined') {
      tempLink.setAttribute('target', '_blank')
    }
    document.body.appendChild(tempLink) // 将a指标添加到body当中
    tempLink.click() // 启动下载
    document.body.removeChild(tempLink) // 下载完毕删除a指标
    window.URL.revokeObjectURL(blobURL)
  }
}

/* 导出图片
 *  refName： 需要转为图片的dom元素
 *  fileName： 导出图片名
 */
export function exportImg(refName, fileName) {
  console.log(refName, 'refName')
  html2canvas(refName, {}).then((canvas) => {
    const imageUrl = canvas.toDataURL('image/png') // 将canvas转成base64图片格式
    downloadFile(imageUrl, fileName)
  })
}

// 二进制流导出处理
export function downloadFile(data, filename) {
  // 转化为blob对象
  const blob = base64ToBlob(data)
  if (typeof window.navigator.msSaveBlob !== 'undefined') { // 兼容IE
    const parts = data.split(';base64,')
    const contentType = parts[0].split(':')[1]
    const imgType = contentType.split('/')[1] // 图片类型png/jpeg
    window.navigator.msSaveBlob(blob, filename + '.' + imgType)
  } else {
    var blobURL = window.URL.createObjectURL(blob) // 将blob对象转为一个URL
    var tempLink = document.createElement('a') // 创建一个a指标
    tempLink.style.display = 'none'
    tempLink.href = blobURL
    tempLink.setAttribute('download', filename) // 给a指标添加下载属性
    if (typeof tempLink.download === 'undefined') {
      tempLink.setAttribute('target', '_blank')
    }
    document.body.appendChild(tempLink) // 将a指标添加到body当中
    tempLink.click() // 启动下载
    document.body.removeChild(tempLink) // 下载完毕删除a指标
    window.URL.revokeObjectURL(blobURL)
  }
}

// 将图片的base64格式转为blob格式
function base64ToBlob(code) {
  const parts = code.split(';base64,')
  const contentType = parts[0].split(':')[1]
  const raw = window.atob(parts[1])
  const rawLength = raw.length

  const uInt8Array = new Uint8Array(rawLength)

  for (let i = 0; i < rawLength; ++i) {
    uInt8Array[i] = raw.charCodeAt(i)
  }
  return new Blob([uInt8Array], { type: contentType })
}

// 增加千分符
export function numFormat(num) {
  var res = num.toString().replace(/\d+/, function(n) { // 先提取整数部分
    return n.replace(/(\d)(?=(\d{3})+$)/g, function($1) {
      return $1 + ','
    })
  })
  return res
}
export function formatMoney(s, q) {
  s = Number(s).toFixed(2)
  var res = s.toString().replace(/\d+/, function(n) { // 先提取整数部分
    return n.replace(/(\d)(?=(\d{3})+$)/g, function($1) {
      return $1 + ','
    })
  })
  return res
}

// 导出图片处理
export function createCanves(headerBody, tableBody, noDelHandle) {
  var div = document.createElement('div')
  div.id = 'html'
  // div.appendChild(headerBody)
  var newDom = tableBody.cloneNode(true)
  newDom.querySelector('.el-table__fixed-right') && newDom.removeChild(newDom.querySelector('.el-table__fixed-right'))
  const tbodyDom = newDom.querySelector('.el-table__body tbody').children
  if (tbodyDom) {
    for (let i = 0; i < tbodyDom.length; i++) {
      // tbodyDom.item(i) && tbodyDom.item(i).removeChild(tbodyDom.item(i).firstChild)
      tbodyDom.item(i) && tbodyDom.item(i).removeChild(tbodyDom.item(i).querySelector('.el-table-column--selection'))
      /* if (!noDelHandle) {
              const childlen = tbodyDom.item(i).children.length
              // console.log(theadDom.item(i).children.item(childlen - 2), 'theadDom.item(i)')
              tbodyDom.item(i) && tbodyDom.item(i).removeChild(tbodyDom.item(i).children.item(childlen - 2))
            } */
    }
  }

  var newDom2 = headerBody.cloneNode(true)
  const newHeader = newDom2.children[1]
  while (newDom2.firstChild) {
    newDom2.removeChild(newDom2.firstChild)
  }
  newDom2.appendChild(newHeader)
  console.log(newDom2, 'header')

  const theadDom = newDom2.querySelector('.el-table__header thead').children
  if (theadDom) {
    for (let i = 0; i < theadDom.length; i++) {
      // theadDom.item(i) && theadDom.item(i).removeChild(theadDom.item(i).firstChild)
      theadDom.item(i) && theadDom.item(i).removeChild(theadDom.item(i).querySelector('.el-table-column--selection'))
      if (!noDelHandle) {
        const childlen = theadDom.item(i).children.length
        // console.log(theadDom.item(i).children.item(childlen - 2), 'theadDom.item(i)')
        theadDom.item(i) && theadDom.item(i).removeChild(theadDom.item(i).children.item(childlen - 2))
      }
    }
  }

  div.appendChild(newDom2)
  div.appendChild(newDom)

  return div
}
// 获取上一年时间
export function lastYear() {
    var today = new Date();
    y = today.getFullYear();
    m = CityAndStoreList.pad(today.getMonth() + 1, 2);
    d = CityAndStoreList.pad(today.getDate(), 2);
    // var SelTime = y + '-' + m + '-' + d;//开始时间……
    return y+1 + '-' + m + '-' + d;//开始时间得到的是2011-7-8
}

export function getUrlParam(name) {
  var url = window.location.search
  const params = url.substr(url.indexOf('?') + 1).split('&')
  // console.log(params,'params');
  for (let i = 0; i < params.length; i++) {
    const param = params[i]
    const key = param.split('=')[0]
    const value = param.split('=')[1]
    if (key === name) {
      return value
    }
  }
}
